﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Data.SqlClient;
using System.Linq;
using System.Text.RegularExpressions;

namespace QuanLyBanVeXe
{

    public partial class FormMain : DevComponents.DotNetBar.Metro.MetroForm
    {
        public SqlConnection myConnection;
        public SqlCommand myCommand;
        SqlDataAdapter myDataAdapter;
        private DataTable dt = new DataTable();

        public FormMain()
        {
            InitializeComponent();
        }
        public static string Username = string.Empty;
        public static string Quyen = string.Empty;

        private void FormMain_Load(object sender, EventArgs e)
        {
            ssCount.Text = "";
            ssThongBao.Text = "Giới thiệu";
            if (!string.IsNullOrEmpty(Username))
                this.lblTaiKhoan.Text = Username;
            if (!string.IsNullOrEmpty(Quyen))
                this.lblQuyen.Text = Quyen;
            sfDongHo.Movie = Application.StartupPath + "\\clock.swf";
            sfNamMoi.Movie = Application.StartupPath + "\\amlich2.swf";
            sfLich.Movie = Application.StartupPath + "\\calendar.swf";
            if (lblQuyen.Text == "Admin")
            {
                try
                {
                    this.Show();
                    cbbLoaiXe.DataSource = LayLoaiXe();
                    cbbLoaiXe.DisplayMember = "LoaiXe";
                    cbbLoaiXe.ValueMember = "LoaiXe";
                    cbbThoiGian.DataSource = LayThoiGian();
                    cbbThoiGian.DisplayMember = "GioXuatBen";
                    cbbThoiGian.ValueMember = "GioXuatBen";
                    cbbLoaiXe.Text = null;
                    cbbMaTuyen.Text = null;
                    lblQuyen.Text = "Admin";
                    mnuDuLieu.Visible = true;
                    mnuQuanLy.Visible = true;
                    btnBaoCao.Visible = true;
                    btnRefresh.Visible = false;
                    
                    cbbTimLoaiXe.DataSource = LayLoaiXe();
                    cbbTimLoaiXe.DisplayMember = "LoaiXe";
                    cbbTimLoaiXe.ValueMember = "LoaiXe";
                    cbbTimLoaiXe.Text = null;

                    cbbTimTuyen.DataSource = LayTuyenDiTim();
                    cbbTimTuyen.DisplayMember = "BenDiBenDen";
                    cbbTimTuyen.ValueMember = "BenDiBenDen";
                    cbbTimTuyen.Text = null;
                    mnuLoad_Click(sender, e);
                }
                catch { }
            }
            else
            {
                try
                {
                    this.Show();
                    cbbLoaiXe.DataSource = LayLoaiXe();
                    cbbLoaiXe.DisplayMember = "LoaiXe";
                    cbbLoaiXe.ValueMember = "LoaiXe";
                    cbbThoiGian.DataSource = LayThoiGian();
                    cbbThoiGian.DisplayMember = "GioXuatBen";
                    cbbThoiGian.ValueMember = "GioXuatBen";
                    cbbLoaiXe.Text = null;
                    cbbMaTuyen.Text = null;
                    lblQuyen.Text = "Guest";
                    mnuDuLieu.Visible = false;
                    mnuQuanLy.Visible = false;
                    btnBaoCao.Visible = false;
                    btnRefresh.Visible = true;
                    
                    cbbTimLoaiXe.DataSource = LayLoaiXe();
                    cbbTimLoaiXe.DisplayMember = "LoaiXe";
                    cbbTimLoaiXe.ValueMember = "LoaiXe";
                    cbbTimLoaiXe.Text = null;

                    cbbTimTuyen.DataSource = LayTuyenDiTim();
                    cbbTimTuyen.DisplayMember = "BenDiBenDen";
                    cbbTimTuyen.ValueMember = "BenDiBenDen";
                    cbbTimTuyen.Text = null;
                    mnuLoad_Click(sender, e);
                }
                catch { }
            }
            

        }
        public void KetNoi()
        {
            string strconnect = @"server=.\SQLEXPRESS; database=QuanLyBanVeXe; integrated security=true";
            myConnection = new SqlConnection(strconnect);
            myCommand = myConnection.CreateCommand();
        }
        private void mnuLoad_Click(object sender, EventArgs e)
        {
            if (string.Compare(lblQuyen.Text, "Admin", true) == 0)
            {
                KetNoi();
                myCommand.CommandType = CommandType.Text;
                myCommand.CommandText = "select  * FROM QuanLyVe";
                try
                {
                    if (this.myConnection.State == ConnectionState.Closed)
                        this.myConnection.Open();
                    myDataAdapter = new SqlDataAdapter(myCommand);
                    DataSet ds = new DataSet();
                    myDataAdapter.Fill(ds, "QuanLyVe");
                    this.dgvDanhSachVe.DataSource = ds.Tables[0];
                    this.myConnection.Close();
                }
                catch
                {
                    MessageBox.Show("Lối kết nối");
                }
                btnThem.Visible = true;
                //ssCount.Text = " || Số lượng vé đã đăng ký: " + DemSoLuongVe().ToString();

                //dtTimNgay.Text = "";
                btnXoa.Enabled = false;
                btnCapNhat.Enabled = false;
                dgvDanhSachVe.Enabled = true;
                this.txtGiaVe.Visible = true;
                txtMaVe.Visible = true;
                
                lblGiaVe.Visible = lblMaVe.Visible = true;
                lblCheckDiaChi.Visible = lblCheckGhe.Visible = lblCheckGT.Visible = lblCheckNgay.Visible = lblCheckSDT.Visible = lblCheckTen.Visible =
                    lblCheckThoiGian.Visible = lblCheckTuoi.Visible = lblCheckTuyenDi.Visible = lblCheckLoaiXe.Visible = false;
            }
            else
            {
                KetNoi();
                myCommand.CommandType = CommandType.Text;
                myCommand.CommandText = "select  * FROM QuanLyVe where TenTK ='" + lblTaiKhoan.Text + "'";
                try
                {
                    if (this.myConnection.State == ConnectionState.Closed)
                        this.myConnection.Open();
                    myDataAdapter = new SqlDataAdapter(myCommand);
                    DataSet ds = new DataSet();
                    myDataAdapter.Fill(ds, "QuanLyVe");
                    this.dgvDanhSachVe.DataSource = ds.Tables[0];
                    this.myConnection.Close();
                }
                catch
                {
                    MessageBox.Show("Lối kết nối");
                }
                btnThem.Visible = true;
                //ssCount.Text = " || Số lượng vé đã đăng ký: " + DemSoLuongVe().ToString();

                //dtTimNgay.Text = "";
                btnXoa.Enabled = false;
                btnCapNhat.Enabled = false;
                dgvDanhSachVe.Enabled = true;
                lblCheckDiaChi.Visible = lblCheckGhe.Visible = lblCheckGT.Visible = lblCheckNgay.Visible = lblCheckSDT.Visible = lblCheckTen.Visible =
                    lblCheckThoiGian.Visible = lblCheckTuoi.Visible = lblCheckTuyenDi.Visible = lblCheckLoaiXe.Visible = false;
            }
        }

        private int DemSoLuongVe()
        {
            return dgvDanhSachVe.RowCount - 1;
        }

        #region=========Lay du lieu cho vao combobox===========
        private void cbbLoaiXe_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbbSoGhe.Items.Clear();
            cbbTuyenDi.DataSource = LayTuyenDi();
            cbbTuyenDi.DisplayMember = "BenDiBenDen";
            cbbTuyenDi.ValueMember = "BenDiBenDen";
            if (cbbLoaiXe.Text == "Ghế")
                for (int i = 1; i <= 45; i++)
                    cbbSoGhe.Items.Add(i);
            else
                for (int i = 1; i <= 40; i++)
                    cbbSoGhe.Items.Add(i);
        }


        private void cbbTuyenDi_TextChanged(object sender, EventArgs e)
        {
            cbbMaTuyen.DataSource = LayMaTuyenDi();
            cbbMaTuyen.DisplayMember = "MaTuyen";
            cbbMaTuyen.ValueMember = "MaTuyen";
        }
        public DataTable LayLoaiXe()
        {
            SqlConnection con = new SqlConnection(@"server=.\SQLEXPRESS; database=QuanLyBanVeXe; integrated security=true");
            DataTable dt = new DataTable();
            string sql = "select LoaiXe from LoaiXe";
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            da.Fill(dt);
            return dt;
        }
        public DataTable LayTuyenDi()
        {
            SqlConnection con = new SqlConnection(@"server=.\SQLEXPRESS; database=QuanLyBanVeXe; integrated security=true");
            DataTable dt = new DataTable();
            string sql = "select BenDiBenDen from TuyenDi where LoaiXe = N'" + cbbLoaiXe.Text + "'";
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            da.Fill(dt);
            return dt;
        }
        public DataTable LayTuyenDiTim()
        {
            SqlConnection con = new SqlConnection(@"server=.\SQLEXPRESS; database=QuanLyBanVeXe; integrated security=true");
            DataTable dt = new DataTable();
            string sql = "select BenDiBenDen from TuyenDi";
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            da.Fill(dt);
            return dt;
        }
        public DataTable TimTuyenDi()
        {
            SqlConnection con = new SqlConnection(@"server=.\SQLEXPRESS; database=QuanLyBanVeXe; integrated security=true");
            DataTable dt = new DataTable();
            string sql = "select BenDiBenDen from TuyenDi where LoaiXe = N'" + cbbLoaiXe.Text + "'";
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            da.Fill(dt);
            return dt;
        }
        public DataTable LayMaTuyenDi()
        {
            SqlConnection con = new SqlConnection(@"server=.\SQLEXPRESS; database=QuanLyBanVeXe; integrated security=true");
            DataTable dt = new DataTable();
            string sql = "select MaTuyen from TuyenDi where BenDiBenDen = N'" + cbbTuyenDi.Text + "'";
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            da.Fill(dt);
            return dt;
        }
        public DataTable LayThoiGian()
        {
            SqlConnection con = new SqlConnection(@"server=.\SQLEXPRESS; database=QuanLyBanVeXe; integrated security=true");
            DataTable dt = new DataTable();
            string sql = "select GioXuatBen from ThoiGian";
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            da.Fill(dt);
            return dt;
        }


        #endregion======================================
        private void MacDinh()
        {
            txtMaVe.Text = "";
            txtHoTen.Text = "";
            txtDiaChi.Text = "";
            txtGiaVe.Text = "";
            txtSDT.Text = "";
            txtTuoi.Text = "";
            rdNam.Checked = rdNu.Checked = false;
            cbbThoiGian.Text = null;
            cbbSoGhe.Text = null;
            cbbTuyenDi.Text = null;
            cbbLoaiXe.Text = null;
            dtNgayXuatBen.Text = "";
            lblCheckDiaChi.Visible = lblCheckGhe.Visible = lblCheckGT.Visible = lblCheckNgay.Visible = lblCheckSDT.Visible = lblCheckTen.Visible =
                lblCheckThoiGian.Visible = lblCheckTuoi.Visible = lblCheckTuyenDi.Visible = lblCheckLoaiXe.Visible = false;
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            mnuLoad_Click(sender, e);
            btnCapNhat.Enabled = btnXoa.Enabled = btTimKiem.Enabled = btnBaoCao.Enabled= false;
            btnThem.Visible = false;
            btnDangKy.Visible = true;
            txtHoTen.Focus();
            dgvDanhSachVe.Enabled = false;
            MacDinh();
            btnXoa.Visible = false;
            btnCapNhat.Enabled = false;
            btHuyThem.Visible = true;
            cbbSoGhe.Items.Clear();
            this.txtGiaVe.Visible = false;
            txtMaVe.Visible = false;
            lblGiaVe.Visible = lblMaVe.Visible = false;
            ssThongBao.Text = "Thêm một vé mới";
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            string gt;
            if (rdNam.Checked == true)
                gt = "Nam";
            else
                gt = "Nữ";
            lblCheckDiaChi.ForeColor = lblCheckGhe.ForeColor = lblCheckGT.ForeColor = lblCheckNgay.ForeColor = lblCheckSDT.ForeColor = lblCheckTen.ForeColor =
                lblCheckThoiGian.ForeColor = lblCheckTuoi.ForeColor = lblCheckTuyenDi.ForeColor = lblCheckLoaiXe.ForeColor = Color.Red;

            if (this.txtHoTen.Text.Trim().Length == 0)
                lblCheckTen.Visible = true;
            else
                lblCheckTen.Visible = false;
            if (this.rdNam.Checked == false && this.rdNu.Checked == false)
                lblCheckGT.Visible = true;
            else
                lblCheckGT.Visible = false;
            if (txtTuoi.Text.Trim().Length == 0 || Convert.ToInt32(txtTuoi.Text) < 14)
                lblCheckTuoi.Visible = true;
            else
                lblCheckTuoi.Visible = false;
            if (this.txtDiaChi.Text.Trim().Length == 0)
                lblCheckDiaChi.Visible = true;
            else
                lblCheckDiaChi.Visible = false;
            if (this.txtSDT.Text.Trim().Length == 0 || this.txtSDT.Text.Trim().Length < 10 || this.txtSDT.Text.Trim().Length > 11)
                lblCheckSDT.Visible = true;
            else
                lblCheckSDT.Visible = false;
            if (this.cbbLoaiXe.Text.Trim().Length == 0)
                lblCheckLoaiXe.Visible = true;
            else
                lblCheckLoaiXe.Visible = false;
            if (this.cbbTuyenDi.Text.Trim().Length == 0)
                lblCheckTuyenDi.Visible = true;
            else
                lblCheckTuyenDi.Visible = false;
            if (this.cbbThoiGian.Text.Trim().Length == 0)
                lblCheckThoiGian.Visible = true;
            else
                lblCheckThoiGian.Visible = false;
            if (this.dtNgayXuatBen.Value < DateTime.Now)
                lblCheckNgay.Visible = true;
            else
                lblCheckNgay.Visible = false;
            if (this.cbbSoGhe.Text.Trim().Length == 0 || KiemTraGhe())
                lblCheckGhe.Visible = true;
            else
                lblCheckGhe.Visible = false;

            if (this.txtHoTen.Text.Trim().Length == 0 && this.rdNam.Checked == false && this.rdNu.Checked == false)
            {
                this.txtHoTen.Focus();
                ssThongBao.Text = "Bạn chưa nhập đủ thông tin!";
                return;
            }
            else
                if (txtTuoi.Text.Trim().Length == 0 || Convert.ToInt32(txtTuoi.Text) < 14)
                {
                    ssThongBao.Text = "Nhập tuổi của bạn!";
                    return;
                }
                else
                    if (this.txtDiaChi.Text.Trim().Length == 0)
                    {

                        this.txtDiaChi.Focus();
                        ssThongBao.Text = "Bạn chưa nhập đủ thông tin!";
                        return;
                    }
                    else
                        if (this.txtSDT.Text.Trim().Length == 0 || this.txtSDT.Text.Trim().Length < 10 || this.txtSDT.Text.Trim().Length > 11)
                        {

                            this.txtSDT.Focus();
                            ssThongBao.Text = "Bạn chưa nhập số điện thoại hoặc số điện thoại không đúng!!";
                            return;
                        }
                        else
                            if (this.cbbTuyenDi.Text.Trim().Length == 0)
                            {

                                this.cbbTuyenDi.Focus();
                                ssThongBao.Text = "Chọn Loại xe và Tuyến đi!";
                                return;
                            }
                            else
                                if (this.cbbThoiGian.Text.Trim().Length == 0)
                                {

                                    this.cbbThoiGian.Focus();
                                    ssThongBao.Text = "Chọn thời gian!";
                                    return;
                                }
                                else
                                    if (this.dtNgayXuatBen.Value < DateTime.Now)
                                    {

                                        this.dtNgayXuatBen.Focus();
                                        ssThongBao.Text = "Ngày xuất bến không hợp lệ.\nNgày xuất bến phải lớn hơn ngày hiện tại";
                                        return;
                                    }
                                    else
                                        if (this.cbbSoGhe.Text.Trim().Length == 0)
                                        {
                                            this.cbbSoGhe.Focus();
                                            ssThongBao.Text = "Chọn số ghế!";
                                            return;
                                        }
                                        else
                                            if (KiemTraGheCapNhat())
                                            {
                                                this.cbbSoGhe.Focus();
                                                ssThongBao.Text = "Số ghế này đã có người đặt!";
                                                return;
                                            }
                                            else
                                            {
                                                try
                                                {
                                                    if (this.myConnection.State == ConnectionState.Closed)
                                                        this.myConnection.Open();
                                                    string ma = dgvDanhSachVe.SelectedRows[0].Cells[1].Value.ToString();
                                                    myCommand.CommandText = "Update VeXe set HoTen=N'" + txtHoTen.Text + "',GioiTinh=N'" + gt + "',Tuoi='" + txtTuoi.Text + "',DiaChi=N'" + txtDiaChi.Text + "',SoDT='" + txtSDT.Text + "',MaTuyen=N'" + cbbMaTuyen.Text + "',ThoiGian='" + cbbThoiGian.Text + "',NgayXuatBen='" + dtNgayXuatBen.Value.ToString("MM/dd/yyyy") + "',GheSo='" + cbbSoGhe.Text + "',TenTK='" + lblTaiKhoan.Text + "' where MaVe = '" + ma + "'";
                                                    myCommand.CommandType = CommandType.Text;
                                                    myCommand.ExecuteNonQuery();
                                                    this.myConnection.Close();
                                                    ssThongBao.Text = "Đã sửa thành công vé xe " + ma;
                                                }
                                                catch
                                                {
                                                    MessageBox.Show("Sửa không thành công");
                                                }
                                            }
            mnuLoad_Click(sender, e);
        }
        private bool KiemTraGhe()
        {
            myCommand = new SqlCommand();
            myCommand.Connection = myConnection;
            myCommand.CommandType = CommandType.Text;
            myCommand.CommandText = @"select * from VeXe";
            myDataAdapter.SelectCommand = myCommand;
            myDataAdapter.Fill(dt);
            bool kq = false;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i][6].ToString().Trim().CompareTo(cbbMaTuyen.Text) == 0 && dt.Rows[i][7].ToString().Trim().CompareTo(cbbThoiGian.Text) == 0
                    && dt.Rows[i][8].ToString().CompareTo(dtNgayXuatBen.Value.ToString()) == 0 && dt.Rows[i][9].ToString().Trim().CompareTo(cbbSoGhe.Text) == 0)
                {
                    kq = true;
                    break;
                }

            }
            return kq;
        }
        private bool KiemTraGheCapNhat()
        {
            myCommand = new SqlCommand();
            myCommand.Connection = myConnection;
            myCommand.CommandType = CommandType.Text;
            myCommand.CommandText = @"select * from VeXe";
            myDataAdapter.SelectCommand = myCommand;
            myDataAdapter.Fill(dt);
            bool kq = false;
            for (int i = 0; i < dt.Rows.Count; i++)
            {

                if (cbbSoGhe.Text.CompareTo(ghe) == 0 && cbbThoiGian.Text.CompareTo(gio) == 0 && cbbTuyenDi.Text.CompareTo(tuyendi) == 0
                    && dtNgayXuatBen.Value.ToString().CompareTo(ngay) == 0 && cbbLoaiXe.Text.CompareTo(loaixe) == 0)
                    return false;
                else
                {
                    if (dt.Rows[i][6].ToString().Trim().CompareTo(cbbMaTuyen.Text) == 0 && dt.Rows[i][7].ToString().Trim().CompareTo(cbbThoiGian.Text) == 0
                        && dt.Rows[i][8].ToString().CompareTo(dtNgayXuatBen.Value.ToString()) == 0 && dt.Rows[i][9].ToString().Trim().CompareTo(cbbSoGhe.Text) == 0)
                    {
                        kq = true;
                        break;
                    }
                }
            }
            return kq;
        }
        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn muốn xóa vé xe đã chọn?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                for (int i = dgvDanhSachVe.SelectedRows.Count - 1; i >= 0; i--)
                {
                    //Lấy dòng chọn trong gridview
                    string ma = dgvDanhSachVe.SelectedRows[i].Cells[1].Value.ToString();
                    if (this.myConnection.State == ConnectionState.Closed)
                        this.myConnection.Open();
                    myCommand.CommandText = "delete from VeXe where MaVe ='" + ma + "'";
                    myCommand.ExecuteNonQuery();
                    myConnection.Close();
                    DataGridViewRow row = this.dgvDanhSachVe.SelectedRows[i];
                    dgvDanhSachVe.Rows.Remove(row);
                }
                ssThongBao.Text = "Đã xóa thành công";
                ssCount.Text = " || Số lượng vé đã đăng ký: " + DemSoLuongVe().ToString();
                MacDinh();

            }
            //mnuLoad_Click(sender, e);
        }

        private void mnuXoaTatCa_Click(object sender, EventArgs e)
        {
            mnuLoad_Click(sender, e);
            if (MessageBox.Show("Bạn muốn xóa tất cả vé xe?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                if (lblTaiKhoan.Text == "Admin")
                {
                    myConnection.Open();
                    myCommand.CommandText = "delete from VeXe";
                    myCommand.ExecuteNonQuery();
                    myConnection.Close();
                    mnuLoad_Click(sender, e);
                    ssThongBao.Text = "Đã xóa tất cả vé xe thành công";
                }
                else
                {
                    myConnection.Open();
                    myCommand.CommandText = "delete from VeXe where TenTK = '"+lblTaiKhoan.Text+"'";
                    myCommand.ExecuteNonQuery();
                    myConnection.Close();
                    mnuLoad_Click(sender, e);
                    ssThongBao.Text = "Đã xóa tất cả vé xe thành công";
                }
            }
        }

        //click vao danh sach
        string ghe, gio, ngay, tuyendi, loaixe;

        private void dgvDanhSachVe_Click(object sender, EventArgs e)
        {

            if (this.dgvDanhSachVe.SelectedRows.Count > 0)
            {
                try
                {

                    for (int i = dgvDanhSachVe.SelectedRows.Count - 1; i >= 0; i--)
                    {

                        tuyendi = dgvDanhSachVe.SelectedRows[i].Cells[7].Value.ToString();
                        gio = dgvDanhSachVe.SelectedRows[i].Cells[8].Value.ToString();
                        ngay = dgvDanhSachVe.SelectedRows[i].Cells[9].Value.ToString();
                        ghe = dgvDanhSachVe.SelectedRows[i].Cells[11].Value.ToString();
                        loaixe = dgvDanhSachVe.SelectedRows[i].Cells[10].Value.ToString();
                    }
                    DataGridViewRow drow = this.dgvDanhSachVe.SelectedRows[0];
                    DataRowView row = drow.DataBoundItem as DataRowView;
                    this.txtMaVe.Text = row[0].ToString();
                    this.txtHoTen.Text = row[1].ToString();
                    if (row[2].ToString() == "Nam")
                        rdNam.Checked = true;
                    else
                        rdNu.Checked = true;
                    this.txtTuoi.Text = row[3].ToString(); ;
                    this.txtDiaChi.Text = row[4].ToString();
                    this.txtSDT.Text = row[5].ToString();
                    this.cbbTuyenDi.Text = row[6].ToString();
                    this.cbbThoiGian.Text = row[7].ToString();
                    this.dtNgayXuatBen.Value = DateTime.Parse(row[8].ToString());
                    this.cbbLoaiXe.Text = row[9].ToString();
                    this.cbbSoGhe.Text = row[10].ToString();
                    txtGiaVe.Text = string.Format("{0:0,0 vnđ}", Convert.ToInt32(row[11].ToString()));
                    //defaul
                    ssThongBao.Text = "Thông báo";
                    gpVeXe.Text = "Chi tiết vé xe";
                    gpTimKiem.Visible = false;
                    btnThem.Enabled = btnDangKy.Enabled = btnXoa.Enabled = btnCapNhat.Enabled = true;

                    btTimKiem.Enabled = true;
                    btnCapNhat.Enabled = btnXoa.Enabled = true;
                    //btnDangKy.Visible = false;
                    btnThem.Visible = true;
                    btnCapNhat.Enabled = true;
                    btnXoa.Enabled = true;
                    btnBaoCao.Enabled = true;
                }
                catch (Exception)
                {

                }
            }

        }

        private void dgvDanhSachVe_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            dgvDanhSachVe.Rows[e.RowIndex].Cells["STT"].Value = e.RowIndex + 1;
        }

        private void dgvDanhSachVe_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dgvDanhSachVe_Click(sender, e);
        }

        private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Dispose();
            Application.Exit();
        }



        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
            Application.Exit();
        }
        private void AnHien(bool isShow)
        {
            btnDangKy.Enabled = txtHoTen.Enabled = txtDiaChi.Enabled = txtSDT.Enabled = txtTuoi.Enabled = cbbLoaiXe.Enabled = txtGiaVe.Enabled = isShow;
            rdNam.Enabled = rdNu.Enabled = dtNgayXuatBen.Enabled = cbbSoGhe.Enabled = cbbThoiGian.Enabled = cbbTuyenDi.Enabled = isShow;

        }

        private void btDangKy_Click(object sender, EventArgs e)
        {
            string gt;
            if (rdNam.Checked == true)
                gt = "Nam";
            else
                gt = "Nữ";
            lblCheckDiaChi.ForeColor = lblCheckGhe.ForeColor = lblCheckGT.ForeColor = lblCheckNgay.ForeColor = lblCheckSDT.ForeColor = lblCheckTen.ForeColor =
                lblCheckThoiGian.ForeColor = lblCheckTuoi.ForeColor = lblCheckTuyenDi.ForeColor = lblCheckLoaiXe.ForeColor = Color.Red;

            if (this.txtHoTen.Text.Trim().Length == 0)
                lblCheckTen.Visible = true;
            else
                lblCheckTen.Visible = false;
            if (this.rdNam.Checked == false && this.rdNu.Checked == false)
                lblCheckGT.Visible = true;
            else
                lblCheckGT.Visible = false;
            if (txtTuoi.Text.Trim().Length == 0 || Convert.ToInt32(txtTuoi.Text) < 14)
                lblCheckTuoi.Visible = true;
            else
                lblCheckTuoi.Visible = false;
            if (this.txtDiaChi.Text.Trim().Length == 0)
                lblCheckDiaChi.Visible = true;
            else
                lblCheckDiaChi.Visible = false;
            if (this.txtSDT.Text.Trim().Length == 0 || this.txtSDT.Text.Trim().Length < 10 || this.txtSDT.Text.Trim().Length > 11)
                lblCheckSDT.Visible = true;
            else
                lblCheckSDT.Visible = false;
            if (this.cbbLoaiXe.Text.Trim().Length == 0)
                lblCheckLoaiXe.Visible = true;
            else
                lblCheckLoaiXe.Visible = false;
            if (this.cbbTuyenDi.Text.Trim().Length == 0)
                lblCheckTuyenDi.Visible = true;
            else
                lblCheckTuyenDi.Visible = false;
            if (this.cbbThoiGian.Text.Trim().Length == 0)
                lblCheckThoiGian.Visible = true;
            else
                lblCheckThoiGian.Visible = false;
            if (this.dtNgayXuatBen.Value < DateTime.Now)
                lblCheckNgay.Visible = true;
            else
                lblCheckNgay.Visible = false;
            if (this.cbbSoGhe.Text.Trim().Length == 0 || KiemTraGhe())
                lblCheckGhe.Visible = true;
            else
                lblCheckGhe.Visible = false;

            if (this.txtHoTen.Text.Trim().Length == 0 && this.rdNam.Checked == false && this.rdNu.Checked == false)
            {
                this.txtHoTen.Focus();
                ssThongBao.Text = "Bạn chưa nhập đủ thông tin!";
                return;
            }
            else
                if (txtTuoi.Text.Trim().Length == 0 || Convert.ToInt32(txtTuoi.Text) < 14)
                {
                    ssThongBao.Text = "Nhập tuổi của bạn!";
                    return;
                }
                else
                    if (this.txtDiaChi.Text.Trim().Length == 0)
                    {

                        this.txtDiaChi.Focus();
                        ssThongBao.Text = "Bạn chưa nhập đủ thông tin!";
                        return;
                    }
                    else
                        if (this.txtSDT.Text.Trim().Length == 0 || this.txtSDT.Text.Trim().Length < 10 || this.txtSDT.Text.Trim().Length > 11)
                        {

                            this.txtSDT.Focus();
                            ssThongBao.Text = "Bạn chưa nhập số điện thoại hoặc số điện thoại không đúng!!";
                            return;
                        }
                        else
                            if (this.cbbTuyenDi.Text.Trim().Length == 0)
                            {

                                this.cbbTuyenDi.Focus();
                                ssThongBao.Text = "Chọn Loại xe và Tuyến đi!";
                                return;
                            }
                            else
                                if (this.cbbThoiGian.Text.Trim().Length == 0)
                                {

                                    this.cbbThoiGian.Focus();
                                    ssThongBao.Text = "Chọn thời gian!";
                                    return;
                                }
                                else
                                    if (this.dtNgayXuatBen.Value < DateTime.Now)
                                    {

                                        this.dtNgayXuatBen.Focus();
                                        ssThongBao.Text = "Ngày xuất bến không hợp lệ.\nNgày xuất bến phải lớn hơn ngày hiện tại";
                                        return;
                                    }
                                    else
                                        if (this.cbbSoGhe.Text.Trim().Length == 0)
                                        {
                                            this.cbbSoGhe.Focus();
                                            ssThongBao.Text = "Chọn số ghế!";
                                            return;
                                        }
                                        else
                                            if (KiemTraGhe())
                                            {

                                                this.cbbSoGhe.Focus();
                                                ssThongBao.Text = "Số ghế này đã có người đặt!";
                                                return;
                                            }
                                            else
                                            {
                                                try
                                                {

                                                    if (this.myConnection.State == ConnectionState.Closed)
                                                        this.myConnection.Open();
                                                    myCommand.CommandText = "insert into VeXe values (N'" + txtHoTen.Text + "',N'" + gt + "','" + txtTuoi.Text + "',N'" + txtDiaChi.Text + "','" + txtSDT.Text + "',N'" + cbbMaTuyen.Text + "','" + cbbThoiGian.Text + "','" + dtNgayXuatBen.Value.ToString("MM/dd/yyyy") + "','" + cbbSoGhe.Text + "','" + lblTaiKhoan.Text + "')";
                                                    myCommand.CommandType = CommandType.Text;
                                                    myCommand.ExecuteNonQuery();
                                                    this.myConnection.Close();
                                                    btnThem.Visible = true;
                                                    btTimKiem.Enabled = true;
                                                    btHuyThem.Visible = false;
                                                    ssThongBao.Text = "Đã đăng ký vé thành công. Cảm ơn quý khách đã đăng ký";
                                                    ssCount.Text = " || Số lượng vé đã đăng ký: " + DemSoLuongVe().ToString();

                                                }
                                                catch
                                                {
                                                    MessageBox.Show("Đăng ký không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                                    ssThongBao.Text = "Thông báo";
                                                }
                                            }
            mnuLoad_Click(sender, e);
        }

        private void btTimKiem_Click(object sender, EventArgs e)
        {
            gpTimKiem.Visible = true;
            gpVeXe.Text = "Tìm kiếm";
            btTimKiem.Enabled = btnBaoCao.Enabled = false;
            ssThongBao.Text = "Nhập dữ liệu cần tìm";
            txtTimMaVe.Focus();
            btnThem.Enabled = btnDangKy.Enabled = btnXoa.Enabled = btnCapNhat.Enabled = false;
        }

        private void refeshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mnuLoad_Click(sender, e);
        }

        //click chuot phai chon row
        private void dgvDanhSachVe_MouseDown(object sender, MouseEventArgs e)
        {
            DataGridView.HitTestInfo Hti;//đối tượng cho biết thông tin hit trên datagridview
            if (e.Button == MouseButtons.Right)
            { //bắt sự kiện chuột phải

                Hti = dgvDanhSachVe.HitTest(e.X, e.Y);//tạo đối tượng HTi
                if (Hti.Type == DataGridViewHitTestType.Cell)
                {//Kiểm tra xem đối tượng HTi có đúng là kiểu Cell không
                    if (!((DataGridViewRow)(dgvDanhSachVe.Rows[Hti.RowIndex])).Selected)
                    {//kiểm tra xem row tại vị trí chuột có đúng là chưa chọn
                        dgvDanhSachVe.ClearSelection();//xóa chọn tất cả trên datagrid
                        ((DataGridViewRow)dgvDanhSachVe.Rows[Hti.RowIndex]).Selected = true;//xác lập chọn cho row tại vị trí nhấn chuột
                        if (this.dgvDanhSachVe.SelectedRows.Count > 0)
                        {
                            try
                            {
                                DataGridViewRow drow = this.dgvDanhSachVe.SelectedRows[0];
                                DataRowView row = drow.DataBoundItem as DataRowView;
                                this.txtHoTen.Text = row[1].ToString();
                                if (row[2].ToString() == "Nam")
                                    rdNam.Checked = true;
                                else
                                    rdNu.Checked = true;
                                this.txtTuoi.Text = row[3].ToString(); ;
                                this.txtDiaChi.Text = row[4].ToString();
                                this.txtSDT.Text = row[5].ToString();
                                this.cbbTuyenDi.Text = row[6].ToString();
                                this.cbbThoiGian.Text = row[7].ToString();
                                this.dtNgayXuatBen.Value = DateTime.Parse(row[8].ToString());
                                this.cbbLoaiXe.Text = row[9].ToString();
                                this.cbbSoGhe.Text = row[10].ToString();
                                this.txtGiaVe.Text = row[11].ToString();
                                ssThongBao.Text = "Thông báo";
                            }
                            catch (Exception)
                            {

                            }
                        }

                    }

                }
            }
        }

        private void smnXoa_Click(object sender, EventArgs e)
        {
            btnXoa_Click(sender, e);
        }



        private void btnCancel_Click(object sender, EventArgs e)
        {
            cbbTimLoaiXe.Text = null;
            mnuLoad_Click(sender, e);
            dgvDanhSachVe_Click(sender, e);

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtTimDiaChi.Text = "";
            txtTimMaVe.Text = "";
            txtTimSDT.Text = "";
            txtTimTen.Text = "";
            cbbTimTuyen.Text = null;
            cbbTimLoaiXe.Text = "";
            dtTimNgay.Text = "";
            cbbTimLoaiXe.Text = null;
            mnuLoad_Click(sender, e);
        }
        //chỉ nhập số
        private void txtTuoi_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void txtSDT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void txtTimSDT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }


        #region ======Tim kiem===========
        private void TimKiemVeAdmin(object sender, EventArgs e)
        {
            
            if (txtTimMaVe.Text.Trim().Length == 0 && txtTimDiaChi.Text.Trim().Length == 0 && txtTimSDT.Text.Trim().Length == 0 && txtTimTen.Text.Trim().Length == 0
                && cbbTimTuyen.Text.Trim().Length == 0 && cbbTimLoaiXe.Text.Trim().Length == 0 && dtTimNgay.Value.ToString("yyyy-MM-dd") == "0001-01-01")
            {
                //ssThongBao.Text = "Nhập dữ liệu cần tìm";

            }
            else
            {
                //this.dgvDanhSachVe.DataSource = null;
                string ngay = dtTimNgay.Value.ToString("yyyy-MM-dd");
                if (ngay == "0001-01-01")
                    ngay = null;
                try
                {
                    myCommand.CommandText = "select * FROM QuanLyVe where HoTen like N'" + "%" + txtTimTen.Text + "%" + "' and DiaChi like N'" + "%" + txtTimDiaChi.Text + "%" + "' and MaVe like '" + "%" + txtTimMaVe.Text + "%" + "' and SoDT like N'" + "%" + txtTimSDT.Text + "%" + "' and BenDiBenDen like N'" + "%" + cbbTimTuyen.Text + "%" + "'and LoaiXe like N'" + "%" + cbbTimLoaiXe.Text + "%" + "' and NgayXuatBen like '" + "%" + ngay + "%" + "'";
                    myCommand.CommandType = CommandType.Text;
                    if (this.myConnection.State == ConnectionState.Closed)
                        this.myConnection.Open();
                    myDataAdapter = new SqlDataAdapter(myCommand);
                    DataSet ds = new DataSet();
                    myDataAdapter.Fill(ds, "QuanLyVe");
                    this.dgvDanhSachVe.DataSource = ds.Tables[0];
                    if (dgvDanhSachVe.RowCount == 1)
                    {
                        ssThongBao.Text = "Không tìm thấy";
                        this.myConnection.Close();
                        //mnuLoad_Click(sender, e);
                    }
                    else
                        ssThongBao.Text = "Đã tìm thấy " + DemSoLuongVe() + " vé";
                }
                catch { }
            }

        }
        private void TimKiemVe(object sender, EventArgs e)
        {
            
            if (txtTimMaVe.Text.Trim().Length == 0 && txtTimDiaChi.Text.Trim().Length == 0 && txtTimSDT.Text.Trim().Length == 0 && txtTimTen.Text.Trim().Length == 0
                && cbbTimTuyen.Text.Trim().Length == 0 && cbbTimLoaiXe.Text.Trim().Length == 0 && dtTimNgay.Value.ToString("yyyy-MM-dd") == "0001-01-01")
            {
                //ssThongBao.Text = "Nhập dữ liệu cần tìm";
            }
            else
            {
                //this.dgvDanhSachVe.DataSource = null;
                string ngay = dtTimNgay.Value.ToString("yyyy-MM-dd");
                if (ngay == "0001-01-01")
                    ngay = null;
                try
                {
                    myCommand.CommandText = "select * FROM QuanLyVe where HoTen like N'" + "%" + txtTimTen.Text + "%" + "' and DiaChi like N'" + "%" + txtTimDiaChi.Text + "%" + "' and MaVe like '" + "%" + txtTimMaVe.Text + "%" + "' and SoDT like N'" + "%" + txtTimSDT.Text + "%" + "' and BenDiBenDen like N'" + "%" + cbbTimTuyen.Text + "%" + "'and LoaiXe like N'" + "%" + cbbTimLoaiXe.Text + "%" + "' and NgayXuatBen like '" + "%" + ngay + "%" + "' and TenTK = '" + lblTaiKhoan.Text + "'";
                    myCommand.CommandType = CommandType.Text;
                    if (this.myConnection.State == ConnectionState.Closed)
                        this.myConnection.Open();
                    myDataAdapter = new SqlDataAdapter(myCommand);
                    DataSet ds = new DataSet();
                    myDataAdapter.Fill(ds, "QuanLyVe");
                    this.dgvDanhSachVe.DataSource = ds.Tables[0];

                    if (dgvDanhSachVe.RowCount == 1)
                    {
                        //MessageBox.Show("Không tìm thấy!");
                        ssThongBao.Text = "Không tìm thấy";
                        this.myConnection.Close();
                        //mnuLoad_Click(sender, e);
                    }
                    else
                        ssThongBao.Text = "Đã tìm thấy " + DemSoLuongVe() + " vé";
                    
                }
                catch { }
            }

        }
        private void txtTimMaVe_TextChanged(object sender, EventArgs e)
        {
            if (lblQuyen.Text == "Admin")
                TimKiemVeAdmin(sender, e);
            else
                TimKiemVe(sender, e);
        }

        private void txtTimTen_TextChanged(object sender, EventArgs e)
        {
            if (lblQuyen.Text == "Admin")
                TimKiemVeAdmin(sender, e);
            else
                TimKiemVe(sender, e);
        }

        private void txtTimDiaChi_TextChanged(object sender, EventArgs e)
        {
            if (lblQuyen.Text == "Admin")
                TimKiemVeAdmin(sender, e);
            else
                TimKiemVe(sender, e);
        }

        private void txtTimSDT_TextChanged(object sender, EventArgs e)
        {
            if (lblQuyen.Text == "Admin")
                TimKiemVeAdmin(sender, e);
            else
                TimKiemVe(sender, e);
        }

        private void txtTimTuyenDi_TextChanged(object sender, EventArgs e)
        {
            if (lblQuyen.Text == "Admin")
                TimKiemVeAdmin(sender, e);
            else
                TimKiemVe(sender, e);
        }

        private void cbbTimLoaiXe_TextChanged(object sender, EventArgs e)
        {
            if (lblQuyen.Text == "Admin")
                TimKiemVeAdmin(sender, e);
            else
                TimKiemVe(sender, e);
        }

        private void dtTimNgay_ValueChanged(object sender, EventArgs e)
        {
            if (lblQuyen.Text == "Admin")
                TimKiemVeAdmin(sender, e);
            else
                TimKiemVe(sender, e);
        }
        private void cbbTimTuyen_TextChanged(object sender, EventArgs e)
        {
            if (lblQuyen.Text == "Admin")
                TimKiemVeAdmin(sender, e);
            else
                TimKiemVe(sender, e);
        }
        #endregion



        private void btHuyThem_Click(object sender, EventArgs e)
        {
            btnXoa.Visible = true;
            btnBaoCao.Enabled = true;
            btnCancel_Click(sender, e);
            btHuyThem.Visible = false;
        }
        private void fontToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dlg = fontDialog1.ShowDialog();
            if (dlg == DialogResult.OK)
            {
                this.dgvBangVe.Font = this.dgvDanhSachVe.Font = this.tabQuanLy.Font = fontDialog1.Font;
            }
        }

        private void colorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dlg = colorDialog1.ShowDialog();
            if (dlg == DialogResult.OK)
            {
                this.dgvBangVe.ForeColor = this.dgvDanhSachVe.ForeColor = this.tabQuanLy.ForeColor = colorDialog1.Color;
            }
        }


        #region ================Xu ly tab Tuyen di va bang gia ve======================
        private void tabTuyenDi_Click(object sender, EventArgs e)
        {
            ssThongBao.Text = "Tuyến đi và bảng vé";

            myCommand.CommandText = "select BenDiBenDen, LoaiXe, GiaVe, PhuThu FROM TuyenDi";
            myCommand.CommandType = CommandType.Text;
            if (this.myConnection.State == ConnectionState.Closed)
                this.myConnection.Open();
            myDataAdapter = new SqlDataAdapter(myCommand);
            DataSet ds = new DataSet();
            myDataAdapter.Fill(ds, "TuyenDi");
            this.dgvBangVe.DataSource = ds.Tables[0];
            ssCount.Text = "Số tuyến: " + (dgvBangVe.RowCount - 1).ToString();
        }
        private void dgvBangVe_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            dgvBangVe.Rows[e.RowIndex].Cells["STTBangVe"].Value = e.RowIndex + 1;
        }
        private void tabVeXe_Click(object sender, EventArgs e)
        {
            mnuLoad_Click(sender, e);
            ssThongBao.Text = "Đặt vé xe";
            ssCount.Text = "Số vé đã đăng ký: " + DemSoLuongVe();
        }
        #endregion
        private void mnuQuanLyTuyenDi_Click(object sender, EventArgs e)
        {
            sfNamMoi.Dispose();
            FormTuyenDi frm = new FormTuyenDi();
            frm.Show();
            this.Hide();
        }
        private void nnuQuanLyLienHe_Click(object sender, EventArgs e)
        {
            sfNamMoi.Dispose();
            FormLienHe frm = new FormLienHe();
            frm.ShowDialog();
            this.Hide();
        }

        private void mnuQuanLyTaiKhoan_Click(object sender, EventArgs e)
        {
            sfNamMoi.Dispose();
            FormTaiKhoan frm = new FormTaiKhoan();
            frm.Show();
            this.Hide();


        }
        private void mnuSaoLuu_Click(object sender, EventArgs e)
        {
            FormSaoLuu frm = new FormSaoLuu();
            frm.ShowDialog();
            string path = frm.DuongDan();
            if (path != ".bak")
            {
                if (MessageBox.Show("Bạn có muốn Sao lưu dữ liệu", "Xác nhận", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    try
                    {
                        KetNoi();
                        this.myConnection.Open();
                        myCommand.Connection = myConnection;
                        myCommand.CommandText = "BACKUP DATABASE [QuanLyBanVeXe] TO DISK='" + path + "'";
                        myCommand.ExecuteNonQuery();
                        myConnection.Close();
                        MessageBox.Show("Backup Database Quản lý bán vé xe thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.ToString(), "BACKUP DATABASE");
                        return;
                    }
                }
            }
        }
        private void mnuPhucHoi_Click(object sender, EventArgs e)
        {
            FormPhucHoi frm = new FormPhucHoi();
            frm.ShowDialog();
            string path = frm.DuongDan();
            if (path.Trim().Length != 0)
            {
                if (MessageBox.Show("Bạn có muốn phục hồi dữ liệu", "Xác nhận", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    try
                    {
                        KetNoi();
                        myConnection.Open();
                        myCommand.Connection = myConnection;
                        myCommand.CommandText = "USE master RESTORE DATABASE [QuanLyBanVeXe] FROM DISK='" + path + "'";
                        myCommand.ExecuteNonQuery();
                        myConnection.Close();
                        MessageBox.Show("Restore Database Quản bán vé xe thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        mnuLoad_Click(sender, e);
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.ToString(), "ERROR RESTORE DATABASE");
                        return;
                    }
                }
            }
        }

        private void mnuDoiMatKhau_Click(object sender, EventArgs e)
        {
            FormDoiMatKhau frm = new FormDoiMatKhau();
            frm.ShowDialog();
        }

        private void mnuLogout_Click(object sender, EventArgs e)
        {
            this.sfNamMoi.Dispose();
            this.Hide();
            Login frm = new Login();
            frm.Show();

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string match = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
            if (this.txtTen.Text.Trim().Length == 0)
            {

                this.txtTen.Focus();
                MessageBox.Show("Nhập họ tên của bạn", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            else
                if (this.txtDiaChiLH.Text.Trim().Length == 0)
                {

                    this.txtDiaChiLH.Focus();
                    MessageBox.Show("Nhập địa chỉ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                    if (this.txtMail.Text.Trim().Length == 0)
                    {
                        this.txtMail.Focus();
                        MessageBox.Show("Nhập Email", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

                    else
                    {
                        Regex reg = new Regex(match);
                        if (!reg.IsMatch(this.txtMail.Text))
                        {
                            this.txtMail.Focus();
                            MessageBox.Show("Email không hợp lệ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                    }

            if (this.txtTieuDe.Text.Trim().Length == 0)
            {
                this.txtTieuDe.Focus();
                MessageBox.Show("Nhập tiêu đề", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
                if (this.txtNoiDung.Text.Trim().Length == 0)
                {
                    this.txtNoiDung.Focus();
                    MessageBox.Show("Nhập nội dung", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                else
                {
                    try
                    {
                        this.myConnection.Open();
                        myCommand.CommandText = "insert into LienHe values (N'" + txtTen.Text + "',N'" + txtDiaChiLH.Text + "','" + txtMail.Text + "',N'" + txtTieuDe.Text + "',N'" + txtNoiDung.Text + "')";
                        myCommand.CommandType = CommandType.Text;
                        myCommand.ExecuteNonQuery();
                        this.myConnection.Close();
                        ssThongBao.Text = "Đã gửi";
                        txtTen.Text = "";
                        txtDiaChiLH.Text = "";
                        txtMail.Text = "";
                        txtNoiDung.Text = "";
                        txtTieuDe.Text = "";
                    }
                    catch
                    {
                        MessageBox.Show("Không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        ssThongBao.Text = "Thông báo";
                    }

                }

            mnuLoad_Click(sender, e);
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            ssThongBao.Text = "Đã hủy gửi liên hệ";
            txtTen.Text = "";
            txtDiaChiLH.Text = "";
            txtMail.Text = "";
            txtNoiDung.Text = "";
            txtTieuDe.Text = "";
        }

        private void btnBaoCao_Click(object sender, EventArgs e)
        {
            ReportVeXe frm = new ReportVeXe();
            ReportVeXe.maso = txtMaVe.Text;
            frm.ShowDialog();
            
        }

        private void tabGioiThieu_Click(object sender, EventArgs e)
        {
            ssThongBao.Text = "Giới thiệu";
        }

        private void tabVanPhong_Click(object sender, EventArgs e)
        {
            ssThongBao.Text = "Văn phòng";
        }

        private void tabLienHe_Click(object sender, EventArgs e)
        {
            ssThongBao.Text = "Liên hệ";
        }
        private bool isActivated = false;
        private void FormMain_Activated(object sender, EventArgs e)
        {
            isActivated = true;
            // tat flash
        }

        private void FormMain_Deactivate(object sender, EventArgs e)
        {
            isActivated = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //sfNamMoi.Stop();
            sfNamMoi.Dispose();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            mnuLoad_Click(sender, e);
        }

       

       

       

       


    }
}